package com.demo.controller;

import ch.qos.logback.core.util.TimeUtil;
import cn.hutool.core.convert.Convert;
import cn.hutool.core.convert.Converter;
import cn.hutool.core.convert.ConverterRegistry;
import cn.hutool.core.convert.NumberChineseFormater;
import cn.hutool.core.lang.Assert;
import cn.hutool.core.lang.TypeReference;
import cn.hutool.core.util.CharsetUtil;

import java.nio.charset.Charset;
import java.util.Date;
import java.util.List;
import java.util.concurrent.TimeUnit;

public class ConvertDemo {

    public static void main(String[] args) {

        //toStr 转换成字符串
        int a = 1;
        String s1 = Convert.toStr(a);
        long[] longs = new long[]{1 , 2, 3, 4 , 5};
        String s = Convert.toStr(longs);
        System.out.println(s +"---" + s1);

        //转换成指定类型数组

        String[] b = {"1" , "2" ,"3"};
        Long[] c = {Long.valueOf(1), Long.valueOf(2), Long.valueOf(3)};

        Integer[] integers = Convert.toIntArray(b);
        String[] strings = Convert.toStrArray(c);

        System.out.println(integers);
        System.out.println(strings);

        System.err.println("----------------------------------------------------------------------");


        //转换为日期对象
        String d = "2022-08-01";
        String e = "2022-08-01 09:42:21";
        String f = "2022-8-1 9:42:21";

        Date date = Convert.toDate(d);
        Date date1 = Convert.toDate(e);
        Date date2 = Convert.toDate(f);

        System.out.println(date +"--" + date1 + "--" + date2);

        //转换为集合

        Object[] aa = {"a" , "你好" ,"世界" , 221};
        List<?> objects = Convert.toList(aa);

        System.out.println(objects);

        //其他转换类型
        Object[] ab = { "a", "你", "好", "", 1 };
        List<String> list = Convert.convert(new TypeReference<List<String>>() {}, ab);
        System.out.println(list);

        //十六进制转换
        String a2 =  "我是一个可爱的字符串";
        String s2 = Convert.toHex(a2, CharsetUtil.CHARSET_UTF_8);
        System.out.println(s2);

        //十六进制转换为字符串
        String s3 = Convert.hexToStr(s2, CharsetUtil.CHARSET_UTF_8);
        System.out.println(s3);


        //中文解码转码
        String a11 = "我不是乱码";
        //转换后result为乱码
        String result = Convert.convertCharset(a11, CharsetUtil.UTF_8, CharsetUtil.ISO_8859_1);
        String raw = Convert.convertCharset(result, CharsetUtil.ISO_8859_1, "UTF-8");
        System.out.println(result);
        System.out.println(raw);

        //时间转换类型
        long a22 = 1999431215;
        //结果为：75  转化成分钟
        long minutes = Convert.convertTime(a22, TimeUnit.MILLISECONDS, TimeUnit.MINUTES);
        long l = Convert.convertTime(a22, TimeUnit.MILLISECONDS, TimeUnit.HOURS);//转换成小时
        long l1 = Convert.convertTime(a22, TimeUnit.MILLISECONDS, TimeUnit.DAYS);//转换成天

        System.out.println(minutes);
        System.out.println(l);
        System.out.println(l1);


        //将金钱转换为汉字
        double a112 = 676.32;
        String digitUppercase = Convert.digitToChinese(a112);
        System.out.println(digitUppercase);

        System.err.println("-----------------------------------------------------------------------");

        //数字转为英文表达
        String format = Convert.numberToWord(100.23);
        System.out.println(format);

        //数字转中文
        // 一万零八百八十九点七二
        String f1 = Convert.numberToChinese(10889.72356, false);

        // 使用金额大写
        // 壹万贰仟陆佰伍拾叁
        String f12 = Convert.numberToChinese(12653, true);
        System.out.println(f1);
        System.out.println(f12);


        //数字中文表示转换为数字
        //壹仟贰佰壹拾贰点叁肆
        String f13 = Convert.numberToChinese(1212.34  , true);
        //一千二百一十二点三四
        String f134 = Convert.numberToChinese(1212.34  , false);

        System.out.println(f13);
        System.out.println(f134);


        //原始类和包装类转换
        //去包装
        Class<?> wrapClass = Integer.class;

        //结果为：int.class
        Class<?> unWraped = Convert.unWrap(wrapClass);
        System.out.println(unWraped);

        //包装
        Class<?> primitiveClass = long.class;

        //结果为：Long.class
        Class<?> wraped = Convert.wrap(primitiveClass);
        System.out.println(wraped);

        ConverterRegistry converterRegistry = ConverterRegistry.getInstance();
        //此处做为示例自定义String转换，因为Hutool中已经提供String转换，请尽量不要替换
        //替换可能引发关联转换异常（例如覆盖String转换会影响全局）
        //todo : 这个类有问题 运行会报错
//        converterRegistry.putCustom(String.class, (Class<? extends Converter<?>>) CustomConverter.class);

        int aced = 454553;
        String result2 = converterRegistry.convert(String.class, aced);
        System.out.println(result2);
    }

}
